package com.fairytask.engine.components.tab
{
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.components.text.Label;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	
	/**
	 * 选项卡组件
	 * tab数据格式:{label:"good"}
	 * @author hanjy
	 */
	public class TabBar extends BaseComponent
	{
		//容器
		private var _tabContainer		:Sprite;
		private var _viewContainer		:Sprite;
		
		//设定
		private var _barWidth			:uint;
		private var _barHeight			:uint;
		private var _viewWidth			:uint;
		private var _viewHeight		:uint;
		private var _viewX				:uint;
		private var _viewY				:uint;
		private var _barSpace			:Number;
		private var _barSkin			:String;
		private var _viewPoint			:Point;
		private var _viewSize			:Rectangle;
		
		//数据
		private var _tabData			:Array	=	[];
		private var _tabItemList		:Array	=	[];
		private var _tabViewList		:Array	=	[];
		private var _currentItem		:TabBarItem;
		private var _currentView		:TabViewItem;
		
		/**
		 * 
		 * @param itemWidth
		 * @param itemHeight
		 * @param itemSpace
		 * @param itemSkin
		 * @param itemList
		 */
		public function TabBar(itemWidth:uint=5,itemHeight:uint=25,itemSpace:Number=0,itemSkin:String="",
							  itemList:Array=null,viewX:int=10,viewY:int=56,viewW:Number=150,viewH:Number=100)
		{
			super();
			_barWidth	=	itemWidth;
			_barHeight	=	itemHeight;
			_barSpace	=	itemSpace;
			_barSkin	=	itemSkin;
			
			_viewPoint		=	new Point(viewX,viewY);
			_viewSize		=	new Rectangle(0,0,viewW,viewH);
			
			initGUI();
			
			tabData		=	itemList;		//tabr分栏的数据
		}
		
		private function initGUI():void
		{
			_tabContainer	=	new Sprite();
			_viewContainer	=	new Sprite();
			addChild(_tabContainer);
			addChild(_viewContainer);
		}
		
		/**
		 * 格式化tabbar显示 
		 * 
		 */
		private function formatList():void
		{
			//清除显示区域
			clearDisplay();
			var itemPos:Number	=	0;
			var isSelected:Boolean	=	false;
			for(var i:int=0;i<_tabData.length;i++)
			{
				var tmpData:Object			=	_tabData[i];
				var tmpItem:TabBarItem	=	new TabBarItem(_tabData[i],_barWidth,_barHeight,_barSkin);
//				if(tmpData.enable!=null)
//				{
//					tmpItem.enabled	=	tmpData.enable;
//				}
				tmpItem.id	=	i;
				tmpItem.x	=	itemPos;
				_tabContainer.addChild(tmpItem);
				itemPos	=	itemPos+tmpItem.width+_barSpace;
				if(tmpItem.enabled)
				{
					tmpItem.addEventListener(MouseEvent.CLICK,setTabSelected);
					tmpItem.addEventListener(MouseEvent.MOUSE_OVER,onItemOver);
				}
				//显示内容
				var tmpView:TabViewItem	=	new TabViewItem(_viewSize.width,_viewSize.height);
				tmpView.id	=	_tabData[i];
				tmpView.visible	=	false;
				_viewContainer.addChild(tmpView);
				
				//数据记录
				_tabViewList.push(tmpView);
				_tabItemList.push(tmpItem);
			}
			
			_viewContainer.x	=	_viewPoint.x;
			_viewContainer.y	=	_viewPoint.y;
			
			selectTabByIndex(0);
		}
		
		/**
		 * 标签over 
		 * @param evt
		 * 
		 */
		private function onItemOver(evt:MouseEvent):void
		{
			var tmpItem	:TabBarItem	=	evt.currentTarget as TabBarItem;
			if(tmpItem.enabled)
			{
				dispatchEvent(new TabEvent(TabEvent.TAB_OVER,tmpItem,null));
			}
		}
		
		private function setTabSelected(e:MouseEvent):void
		{
			if(_currentItem	==	e.currentTarget)
			{
				_tabContainer.addChildAt(_currentItem,_tabContainer.numChildren-1);
				_currentItem.selected	=	true;
				_currentItem.visible	=	true;
				_currentView	=	tmpView;
				return;
			}
			_currentItem	=	e.currentTarget as TabBarItem;
			if(!_currentItem.enabled)
			{
				dispatchEvent(new TabEvent(TabEvent.TAB_CLICK_DISABLED,tmpItem,null));
			}
			else
			{
				for(var i:int=0;i<_tabItemList.length;i++)
				{
					var tmpItem:TabBarItem	=	_tabItemList[i] as TabBarItem;
					var tmpView:TabViewItem	=	_tabViewList[i] as TabViewItem;
					if(_currentItem != tmpItem)
					{
						tmpView.visible	=	false;
						tmpItem.selected=	false;
					}else{
						_tabContainer.addChildAt(tmpItem,_tabContainer.numChildren-1);
						tmpItem.selected=	true;
						tmpView.visible	=	true;
						_currentView	=	tmpView;
					}
				}
				dispatchEvent(new TabEvent(TabEvent.TAB_CLICK,_currentItem,_currentView));
			}
		}
		
		private function clearDisplay():void
		{
			while(_tabContainer.numChildren>0)
			{
				_tabContainer.removeChildAt(0);
			}
			while(_viewContainer.numChildren>0)
			{
				_viewContainer.removeChildAt(0);
			}
		}
		
		private function checkIndex(val:uint):Boolean
		{
			return -1<val<_tabItemList.length;
		}
		
		public function selectTabByIndex(val:uint):void
		{
			if(checkIndex(val))
			{
				var tmpItem:TabBarItem	=	_tabItemList[val] as TabBarItem;
				if(tmpItem)
				{
					if(tmpItem.enabled)
					{
						//trace("Here");
						tmpItem.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
					}
					else
					{
						selectTabByIndex(val+1)
					}
				}
			}
		}
		
		public function getViewByIndex(val:uint):TabViewItem
		{
			var tmpView:TabViewItem;
			if(checkIndex(val))
			{
				tmpView	=	_tabViewList[val];
			}
			return tmpView;
		}
		
		public function getTabByIndex(val:uint):TabBarItem
		{
			var tmpItem:TabBarItem;
			if(checkIndex(val))
			{
				tmpItem	=	_tabItemList[val];
			}
			return tmpItem;
		}
		
		public function addItem(val:Object):void
		{	
			if(_tabData == null)_tabData = [];
			if(val!=null)
			{
				_tabData.push(val);
				formatList();
			}
		}
		
		public function removeItem():void
		{
			if(_tabData!=null)
			{
				_tabData.pop();
				formatList()
			}
		}
		/**
		 * 
		 * @return 
		 */
		public function get tabData():Array
		{
			return _tabData;
		}

		/**
		 * 
		 * @param value
		 */
		public function set tabData(value:Array):void
		{
			_tabData = value;
			if(_tabData!=null)
			{
				formatList();
			}
		}

		/**
		 * 
		 * @param value
		 */
		public function set barWidth(value:uint):void
		{
			_barWidth = value;
		}

		/**
		 * 
		 * @param value
		 */
		public function set barHeight(value:uint):void
		{
			_barHeight = value;
		}

		/**
		 * 
		 * @param value
		 */
		public function set barSpace(value:uint):void
		{
			_barSpace = value;
			formatList();
		}

		/**
		 * 
		 * @param value
		 */
		public function set barSkin(value:String):void
		{
			_barSkin = value;
			formatList();
		}

		public function get viewSize():Rectangle
		{
			return _viewSize;
		}

		public function set viewSize(value:Rectangle):void
		{
			_viewSize = value;
			formatList();
		}

		public function get viewPoint():Point
		{
			return _viewPoint;
		}

		public function set viewPoint(value:Point):void
		{
			_viewPoint = value;
			formatList();
		}

		public function get currentView():TabViewItem
		{
			return _currentView;
		}

		public function set currentView(value:TabViewItem):void
		{
			_currentView = value;
		}

		public function get currentItem():TabBarItem
		{
			return _currentItem;
		}

		public function set currentItem(value:TabBarItem):void
		{
			_currentItem = value;
		}


	}
}